package com.smedi.ismedi.inmail.core.application.service.impl;

import com.smedi.ismedi.inmail.core.application.command.MessageInfoCommand;
import com.smedi.ismedi.inmail.core.application.service.MessageApplicationService;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.MessageInfo;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.MessageRecord;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.announcement.AttachmentDetailInfo;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.announcement.QueriedCriteria;
import com.smedi.ismedi.inmail.core.domain.repository.AttachmentDetailRepository;
import com.smedi.ismedi.inmail.core.domain.service.AnnouncementDomainService;
import com.smedi.ismedi.inmail.core.domain.service.MessageDomainService;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@AllArgsConstructor
public class MessageApplicationServiceImpl implements MessageApplicationService {
    private final DozerUtil dozerUtil;

    private final MessageDomainService messageDomainService;
    private final AttachmentDetailRepository attachmentDetailRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MessageRecord> sendMessage(List<MessageInfoCommand> messages, List<Long> userIds) {
        List<MessageInfo> messageRecords = dozerUtil.convertor(messages, MessageInfo.class);
        return messageDomainService.sendMessage(messageRecords, userIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MessageRecord> readMessages(List<Long> messageIds) {
        List<MessageRecord> messageRecords = messageDomainService.readMessages(messageIds);

        // 加载附件
        for (MessageRecord messageRecord : messageRecords) {
            Long sourceFromId = messageRecord.getSourceFromId();
            String messageType = messageRecord.getMessageType();
            if (messageType.equals("通知公告")) {
                AttachmentDetailInfo attachmentDetailInfo = attachmentDetailRepository.doSelectByAnnouncementId(sourceFromId);
                messageRecord.setAttachmentKeyList(attachmentDetailInfo.getAttachmentKeyList());
                messageRecord.setAttachmentNameList(attachmentDetailInfo.getAttachmentNameList());
                messageRecord.setAttachmentExtensionList(attachmentDetailInfo.getAttachmentExtensionList());
            }
        }

        return messageRecords;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageRecord queryMessages(Long messageId) {
        MessageRecord messageRecord = messageDomainService.queryMessages(messageId);

        // 加载附件
        Long sourceFromId = messageRecord.getSourceFromId();
        String messageType = messageRecord.getMessageType();
        if (messageType.equals("通知公告")) {
            AttachmentDetailInfo attachmentDetailInfo = attachmentDetailRepository.doSelectByAnnouncementId(sourceFromId);
            messageRecord.setAttachmentKeyList(attachmentDetailInfo.getAttachmentKeyList());
            messageRecord.setAttachmentNameList(attachmentDetailInfo.getAttachmentNameList());
            messageRecord.setAttachmentExtensionList(attachmentDetailInfo.getAttachmentExtensionList());
        }

        return messageRecord;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteMessages(List<Long> messageIds) {
        return messageDomainService.deleteMessages(messageIds);
    }
}
